//
//  EQLoginViewController.swift
//  EQQuestionBank
//
//  Created by 范伟健 on 2018/8/3.
//  Copyright © 2018年 FanWeiJian. All rights reserved.
//

import UIKit
import RxSwift

class EQLoginViewController: UIViewController {

    @IBOutlet weak var userTF: UITextField!
    
    @IBOutlet weak var passwordTF: UITextField!
    
    @IBOutlet weak var loginButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        userTF.text = "csy"
        passwordTF.text = "123456789"
        setupUI()
    }
    
    
    private func setupUI(){
        loginButton.layer.cornerRadius = 5
        passwordTF.eq.addLineAtAutoLayout([.EQAddBottomLine], 1, .gray)
        userTF.eq.addLineAtAutoLayout([.EQAddBottomLine], 1, .gray)
    }

    @IBAction func clickLoginButton(_ sender: UIButton) {
        //ms65482 pwd789453
        eqProvider.rx.request(EQService.userLogin(name: userTF.text!, password: passwordTF.text!))
            .filterSuccessfulStatusAndRedirectCodes()
            .subscribeOn(MainScheduler.instance)
            .mapJSON()
            .subscribe(onSuccess: { (res) in
                print(res)
                EQHUD.showSuccess("登录成功")
                self.gotoMainVC()
            },onError: {(error) in
                EQHUD.showFail("账号或密码错误")
            }).disposed(by: rx.disposeBag)
        
    }
    
    private func gotoMainVC(){
        let mainSB = UIStoryboard(name: "Main", bundle: nil)
        let mainVC : UIViewController! = mainSB.instantiateInitialViewController()
        mainVC.modalTransitionStyle = .crossDissolve
        mainVC.modalPresentationStyle = .overCurrentContext
        self.present(mainVC!, animated: true) {
            let window = UIApplication.shared.keyWindow
            window?.rootViewController = mainVC
            window?.makeKeyAndVisible()
        }
    }
}
